﻿$(function () {

    var _url = "action.aspx";
    var info_id = $("input[name=info_id]").val();


    KindEditor.ready(function (K) {
        KE = K.create('#content', {
            resizeType: 1,
            allowPreviewEmoticons: false,
            allowImageUpload: false,
            items: [
				'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
				 'strikethrough', 'table', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
				'insertunorderedlist', '|', 'image', 'link', 'source']
        });

        if (info_id != "") {
            $.ajax({
                url: _url,
                data: { action: "get", id: info_id },
                dataType: "json",
                success: function (data) {
                    $("input[name=title]").val(data.Title);
                    $("select[name=category]").val(data.Category);
                    $("select[name=category]").val(data.Category);
                    $("select[name=subcategory]").val(data.SubCategory);
                    $("select[name=area]").val(data.Area);
                    var acttime = data.TimeDes.split('^');
                    $("input[name=starttime]").val(acttime[0]);
                    $("input[name=endtime]").val(acttime[1]);
                    $("input[name=address]").val(data.Address);
                    $("input[name=price]").val(data.Price);
                    $("textarea[name=shortdes]").val(data.ShortDes);
                    $(".thumimg").attr("src", data.Thumn);
                    KE.html(decodeURIComponent(data.Detail));
                }

            });
        }
    });

    $("#save").click(function () {
        var title = $.trim($("input[name=title]").val());
        if (title == "") {
            alert("请输入标题。")
            return false;
        }
        var postdata = $(".tab input,.tab select,.tab textarea").serializeArray();
        postdata.push({ name: "imgUrl", value: $(".thumimg").attr("src") });
        postdata.push({ name: "content", value: encodeURIComponent(KE.html()) });
        postdata.push({ name: "action", value: "save" });
        postdata.action = "save";
        $.ajax({
            url: "action.aspx",
            type: "POST",
            data: postdata,
            success: function (data, state) {
                location = "info_list.aspx";
            },
            error: function (req, state, err) {
                alert("提交出错。");
                $("body").append(req.responseText);
            },
            beforeSend: function () {
                $("#save,.loading2").toggle();
            },
            complete: function () {
                $("#save,.loading2").toggle();
            }
        });
    });

    $("select[name=category]").change(function () {
        var next = $("select[name=subcategory]");
        if ($(this).val() == "房产资讯") {
            next.show();
        } else {
            next.hide();
        }
    });

    $(".selimg").click(function () {
        var imgurl = window.prompt("请输入预览图地址。");
        if (imgurl != null) {
            $(".thumimg").attr("src", imgurl);
        }
    });

})